//
//  UILoginTextField.m
//  SMealUserSide
//
//  Created by apple on 2018/2/18.
//  Copyright © 2018年 yaozhengzheng. All rights reserved.
//

#import "UILoginTextField.h"
#import <Masonry.h>


#define UIPassWordTextField_DefaultLeftRightMargin  5

#define UIPassWordTextField_DefaultUpdowmMargin  5

#define UIPassWordTextField_RightImgViewWidthHeight  24

@interface UILoginTextField()

@property (nonatomic ,assign) BOOL isRequiredImageV;

@end

@implementation UILoginTextField

+ (UILoginTextField *)loginTextFieldWithImageView:(BOOL)isRequired
{
    UILoginTextField *password = [[UILoginTextField alloc]initWithFrame:CGRectZero requireImageView:isRequired];
    
    return password;
}

- (instancetype)initWithFrame:(CGRect)frame requireImageView:(BOOL)isRequired
{
    if(self = [super initWithFrame:frame])
    {
        self.isRequiredImageV = isRequired;
        
        [self addSubview:self.passwordTextField];
        if(self.isRequiredImageV){
            [self addSubview:self.rightEyeButton];
        }
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    WS(ws);
    if(self.isRequiredImageV){
        [self.rightEyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(ws);
            make.width.height.mas_equalTo(UIPassWordTextField_RightImgViewWidthHeight);
            make.right.equalTo(ws).offset(-UIPassWordTextField_DefaultLeftRightMargin);
        }];
        
        [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(ws);
            make.right.equalTo(ws.rightEyeButton.mas_left).offset(-UIPassWordTextField_DefaultLeftRightMargin);
            make.left.equalTo(ws).offset(UIPassWordTextField_DefaultLeftRightMargin+ws.offset.horizontal);
        }];
        
    }else{
        [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(ws);
            make.left.equalTo(ws).offset(UIPassWordTextField_DefaultLeftRightMargin+ws.offset.horizontal);
            make.right.equalTo(ws).offset(-UIPassWordTextField_DefaultLeftRightMargin);
        }];
    }
    
    
    
}



#pragma mark - lazy
- (UIPlaceHolderTextField *)passwordTextField
{
    if(_passwordTextField == nil){
        _passwordTextField = [[UIPlaceHolderTextField alloc]init];
        _passwordTextField.textColor = [UIColor blackColor];
        _passwordTextField.font = [UIConstantFont getFontW6_H13];
        _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTextField.placeHolderColor = ZBColorChangeRGB(0xc5c9cc, 1.0);
    }
    return _passwordTextField;
}
- (UIButton *)rightEyeButton
{
    if(_rightEyeButton == nil){
        _rightEyeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightEyeButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _rightEyeButton.imageView.clipsToBounds = YES;
        _rightEyeButton.adjustsImageWhenHighlighted = NO;
        [_rightEyeButton setImage:[UIImage imageNamed:@"kejian"] forState:UIControlStateNormal];
        [_rightEyeButton setImage:[UIImage imageNamed:@"bukejian"] forState:UIControlStateSelected];
    }
    return _rightEyeButton;
}

@end
